package com.hzw.saas.web.app.news.controller;

import java.util.Objects;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.news.INewsService;
import com.hzw.saas.api.news.bo.NewsBO;
import com.hzw.saas.api.news.bo.NewsContentBO;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author EZ09
 * @since 07/06/2021
 */
@Controller
@Validated
@RequiredArgsConstructor
@Slf4j
@Api(tags = "新闻中心")
public class NewsController {

    private final INewsService newsService;

    @GetMapping("/news")
    @SysLog
    @ApiOperation(value = "获取新闻列表", notes = "分页获取新闻列表，按时间倒序排序")
    public ResponseEntity<IPage<NewsBO>> pageNews(PageParam page) {
        IPage<NewsBO> pageNews = newsService.pageNews(page.convert());
        return ResponseEntity.ok(pageNews);
    }

    @GetMapping("/news/{newsId}")
    @SysLog
    @ApiOperation(value = "获取新闻详情", notes = "获取新闻详情，含内容")
    public ResponseEntity<NewsContentBO> getNews(@PathVariable String newsId) {
        NewsContentBO newsContent = newsService.getNewsContent(newsId);
        AssertUtil.assertThrow("待查看的新闻不存在", Objects.isNull(newsContent));
        return ResponseEntity.ok(newsContent);
    }
}
